<?php

/*
  | --------------------------------------------------------------------------------------------------
  | <menu>
  |
  | Desenvolvedor:	Eduardo Sirangelo
  | E-mail:		sirangelos@ibest.com.br
  | Descrição:
  |	-
  | Quality is Everyting
  | --------------------------------------------------------------------------------------------------
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Menu {
    
    function menu_principal() { 
        
        $CI =& get_instance();
        $CI->load->model('main_model');
        
        if($CI->uri->segment(3) != "") {
            $id_menu    = $CI->uri->segment(3);
        } elseif($CI->uri->segment(2) != "") {
            $id_menu    = $CI->uri->segment(2);
        } else {
            $id_menu    = 1;    
        }
        
        $array_id_menu = explode(".", $id_menu);
        if(isset ($array_id_menu[0]) && !empty ($array_id_menu[0])) {
            $id_menu_1 = $array_id_menu[0];
        } else {
            $id_menu_1 = 0;
        }
        
        if(isset ($array_id_menu[1]) && !empty ($array_id_menu[1])) {
            $id_menu_2 = $array_id_menu[0] . "." . $array_id_menu[1];
        } else {
            $id_menu_2 = 0;
        }
        
        if(isset ($array_id_menu[2]) && !empty ($array_id_menu[2])) {
            $id_menu_3 = $array_id_menu[0] . "." . $array_id_menu[1] . "." . $array_id_menu[2];
        } else {
            $id_menu_3 = 0;
        }
        
        $menu_html                  = "<ul class='clear'>";
        
        $query                      = $CI->main_model->get_menu_by_where(array("menu_level" => 1));
        
        if($query->num_rows() > 0) {
            
            foreach($query->result() as $row) {
                
                if($id_menu_1 == $row->id_menu) {
                    $class = "class='active'";
                } else {
                    $class = "";
                }
                
                $menu_html .= "<li $class>";
                $menu_html .= anchor($row->url  . $row->id_menu ,$row->menu);
                
                $query0 = $CI->main_model->get_menu_by_where(array("id_menu_superior" => $row->id_menu,"menu_level" => 2));
                if($query0->num_rows() > 0) {
                    
                    $menu_html .= "<ul>";
                    
                    foreach($query0->result() as $row0) {
                        
                        if($id_menu_2  == $row0->id_menu) {
                            $class0 = "class='active'";
                        } else {
                            $class0 = "";
                        }
                
                        $menu_html .= "<li $class0>";
                        $menu_html .= anchor($row0->url . $row0->id_menu,$row0->menu);
                        
                        $query1 = $CI->main_model->get_menu_by_where(array("id_menu_superior" => $row0->id_menu,"menu_level" => 3));
                        if($query1->num_rows() > 0) {
                            
                            $menu_html .= "<ul>";
                            
                            foreach($query1->result() as $row1) {
                                
                                if($id_menu_3  == $row1->id_menu) {
                                    $class1 = "class='active'";
                                } else {
                                    $class1 = "";
                                }
                                
                                $menu_html .= "<li $class1>";
                                $menu_html .= anchor($row1->url . $row1->id_menu,$row1->menu);
                                $menu_html .= "</li>";
                            }
                            
                            $menu_html .= "</ul>";
                            $menu_html .= "</li>";
                            
                        } else {
                            
                            $menu_html .= "<li>";
                            
                        }
                        
                    }
                    
                    $menu_html .= "</ul>";
                    $menu_html .= "</li>";
                    
                } else {
                    
                    $menu_html .= "</li>";
                    
                }                
            }
        }
        
        $menu_html                 .= "</ul>";
        
        return $menu_html;
    }
    
    function check_permitions() {
        
        $CI =& get_instance();
        $CI->load->model('main_model');
        
        $url = "/" . $CI->uri->segment(1) . "/" . $CI->uri->segment(2) . "/";
        $where          = array("url" => "$url");
        $query          = $CI->main_model->get_programa_by_where($where);
        
        if($query->num_rows() > 0) {
            $row            = $query->row();
            $id_menu        = $row->id_menu_programa;
            $id_usuario     = $CI->session->userdata('id_usuario');
            $query0         = $CI->main_model->check_permitions_by_where(array("id_usuario" => $id_usuario,"id_menu" => "$id_menu"));
        
            if($query0->num_rows() == 0) {
                redirect('/main/no_permition/1', 'location');
            } else {
                $row0 = $query0->row();
                return $row0->permissao;
            }
        
        } else {
            redirect('/main/no_permition/2', 'location');
        }
        
    }
}
?>
